package com.guyaning.deyson_core.core.ui.loader;

import android.content.Context;
import android.support.v7.app.AppCompatDialog;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.guyaning.deyson_core.core.R;
import com.guyaning.deyson_core.core.utils.dimen.DimenUtils;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/4/18.
 */

public class LatteLoader {

    public static  final int LOADER_SCALL_SIZE = 8;

    public static  final int LOADER_OFFSET_SIZE = 10;

    public static  final ArrayList<AppCompatDialog>  LOADERS = new ArrayList<>();

    public static  final String  DEFULT_LOADER_STYLE = LoaderStyle.BallSpinFadeLoaderIndicator.name();

    public static void showLoading(String type , Context context){

        final AppCompatDialog appCompatDialog = new AppCompatDialog(context, R.style.dialog);

        final AVLoadingIndicatorView avLoadingIndicatorView = LoaderCreator.create(type,context);

        appCompatDialog.setContentView(avLoadingIndicatorView);

        //设置宽高
        int  deviceWidth = DimenUtils.getScreenWidth();

        int  deviceHeight  = DimenUtils.getScreenHeight();

        Window  dialogWindow = appCompatDialog.getWindow();

        if(dialogWindow!=null){

            WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();

            layoutParams.width = deviceWidth / LOADER_SCALL_SIZE;

            layoutParams.height = deviceHeight / LOADER_SCALL_SIZE;

            layoutParams.height = layoutParams.height + deviceHeight / LOADER_OFFSET_SIZE;

            layoutParams.gravity = Gravity.CENTER;
        }

        LOADERS.add(appCompatDialog);

        appCompatDialog.show();

    }

    public static void  showLoading(Context context){
        showLoading(DEFULT_LOADER_STYLE,context);
    }

    public static void  showLoading(Context context,Enum<LoaderStyle> type){
        showLoading(type.name(),context);
    }

    public static void  stopLoading(){

        for (AppCompatDialog dialog: LOADERS) {
             if(dialog!=null){
               if(dialog.isShowing()){
                   dialog.cancel();
               }
            }
        }
    }

}
